package com.hpdz.hotel_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpdz.hotel_back.entity.Order;
import com.hpdz.hotel_back.mapper.OrderMapper;
import com.hpdz.hotel_back.service.OrderService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Boolean change(List<Integer> orders, String cancel) {
        try{
            for (Integer order : orders) {
                Order byId = getById(order);
                byId.setState(cancel);
                updateById(byId);
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public IPage<Order> listAllOrder(Integer current, Integer size) {
        Page<Order> orderPage = new Page<>(current,size);
        return orderMapper.selectPage(orderPage,null);
    }

    @Override
    public IPage<Order> findByState(Integer current, Integer size, Order order) {
        Page<Order> orderPage = new Page<>(current,size);
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(order.getState() != null,Order::getState,order.getState());
        return orderMapper.selectPage(orderPage,wrapper);
    }
}
